package com.pro.addresslist.ui.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

import com.pro.addresslist.R;
import com.pro.addresslist.dal.bll.IPMACBLL;
import com.pro.addresslist.dal.model.IPMAC;
import com.pro.addresslist.ui.adapter.CommonAdapter;
import com.pro.addresslist.ui.adapter.ViewHolder;
import com.pro.addresslist.ui.common.BaseActivity;

public class MacAddressActivity extends BaseActivity {
	private ListView mac_list;
	private CommonAdapter<IPMAC> macAddress;
	private List<IPMAC> macaddress = new ArrayList<IPMAC>();
	private String MacId;
	public int MID;
	private int posiont;
	private int num;

	@Override
	public int getLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_mac;
	}

	public void init() {
		mac_list = (ListView) findViewById(R.id.list_mac);

	}

	// 接收Mac地址的广播
	public class ReciveBroadcast extends BroadcastReceiver {

		@SuppressLint("NewApi")
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (action == "lbs_SelectPhone") {
				macaddress.clear();
				// 得到广播中得到的数据，并显示出来
				List<IPMAC> list = (List<IPMAC>) intent.getSerializableExtra("parameter");
				for (IPMAC mac : list) {
					macaddress.add(mac);
				}
				num = list.size();
				macAddress.notifyDataSetChanged();
				Log.i("bt", num+"");
			}
			// 解除绑定
			if (action == "lbs_DelPhone") {
				String macID = (String) intent
						.getSerializableExtra("parameter");
				if (macID.equals("succeed")) {
					Toast.makeText(mContext, "解除绑定成功！", Toast.LENGTH_SHORT)
							.show();
					macaddress.remove(posiont);
					// 更新适配器
					macAddress.notifyDataSetChanged();
				} else {
					Toast.makeText(mContext, "解除绑定失败！", Toast.LENGTH_SHORT)
							.show();
				}
			}
			if (action == "lbs_UpdataPhone") {
				// 得到广播中得到的数据，并显示出来
				String macID = (String) intent
						.getSerializableExtra("parameter");
				if (macID.equals("succeed")) {
					IPMACBLL.SelectPhone(mContext);
					Toast.makeText(mContext, "更改成功！", Toast.LENGTH_SHORT)
							.show();
				} else {
					Toast.makeText(mContext, "更改失败！", Toast.LENGTH_SHORT)
							.show();
				}
			}
			if (action == "lbs_AddPhone") {
				// 得到广播中得到的数据，并显示出来
				String macID = (String) intent
						.getSerializableExtra("parameter");
				if (macID.equals("succeed")) {
					IPMACBLL.SelectPhone(mContext);
					Toast.makeText(mContext, "添加成功！", Toast.LENGTH_SHORT)
							.show();
				} else {
					Toast.makeText(mContext, "添加失败！", Toast.LENGTH_SHORT)
							.show();
				}
			}
		}
	}

	@Override
	public void OnActCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		init();
		setTtile("绑定手机");
		setRightText("绑定", new OnClickListener() {
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				LayoutInflater factory1 = LayoutInflater
						.from(MacAddressActivity.this);
				// 获取对话框中的EditText控件中的值
				final View textEntryView1 = factory1.inflate(
						R.layout.activity_macaddress, null);
				final EditText decipt = (EditText) textEntryView1
						.findViewById(R.id.etdecipt);
				final EditText etimei = (EditText) textEntryView1
						.findViewById(R.id.etimei);
				if (num == 0) {
					/**
					 * 获取本机序列号
					 */
					TelephonyManager phman = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
					String IMEI = phman.getDeviceId().trim();// 获取智能设备唯一编号
					etimei.setText(IMEI);
				}
				AlertDialog.Builder add1 = new AlertDialog.Builder(
						MacAddressActivity.this, R.style.my_dialog);
				add1.setTitle("添加IMEI");
				add1.setView(textEntryView1);
				add1.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
								String depict = decipt.getText().toString();
								String imei = etimei.getText().toString();
								IPMAC ipmac = new IPMAC();
								ipmac.set_Depict(depict);
								ipmac.set_MACorTEL(imei);
								IPMACBLL.AddPhone(mContext, ipmac);
							}
						});
				add1.setNegativeButton("取消",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int i) {
							}
						});
				AlertDialog dialogsAlertDialog = add1.create();
				dialogsAlertDialog.show();// 显示对话框
				MacAddressActivity.dialogTitleLineColor(dialogsAlertDialog,
						R.color.color_theme);
			}
		});
		macAddress = new CommonAdapter<IPMAC>(MacAddressActivity.this,
				macaddress, R.layout.macaddress_item) {

			@Override
			public void convert(ViewHolder holder, IPMAC t) {
				// TODO Auto-generated method stub
				holder.setText(R.id.txt_macaddress, t.get_Depict());
				holder.setText(R.id.txt_phone_type, t.get_MACorTELEn());
			}
		};
		mac_list.setAdapter(macAddress);
		ReciveBroadcast receive = new ReciveBroadcast();
		IntentFilter filter = new IntentFilter();
		filter.addAction("lbs_SelectPhone");
		filter.addAction("lbs_DelPhone");
		filter.addAction("lbs_UpdataPhone");
		filter.addAction("lbs_AddPhone");
		registerReceiver(receive, filter);
		IPMACBLL.SelectPhone(mContext);
		mac_list.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				posiont = arg2;
				MacId = macaddress.get(posiont).get_IPMACID();
				mac_list.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
					@Override
					public void onCreateContextMenu(ContextMenu arg0,
							View arg1, ContextMenuInfo arg2) {
						MenuInflater inflator = new MenuInflater(
								MacAddressActivity.this);
						inflator.inflate(R.menu.relation_menu, arg0);
					}
				});
				return false;
			}
		});
	}

	// 长按菜单响应函数
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.del:
			IPMACBLL.DelPhone(mContext, MacId);
			break;
		case R.id.updata:
			LayoutInflater factory1 = LayoutInflater
					.from(MacAddressActivity.this);
			// 获取对话框中的EditText控件中的值
			final View textEntryView1 = factory1.inflate(
					R.layout.activity_macaddress, null);
			final EditText decipt = (EditText) textEntryView1
					.findViewById(R.id.etdecipt);
			final EditText etimei = (EditText) textEntryView1
					.findViewById(R.id.etimei);
			decipt.setText(macaddress.get(posiont).get_Depict());
			etimei.setText(macaddress.get(posiont).get_MACorTELEn());
			AlertDialog.Builder add1 = new AlertDialog.Builder(
					MacAddressActivity.this);
			add1.setTitle("修改IMEI");
			add1.setView(textEntryView1);
			add1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int i) {
					String depict = decipt.getText().toString();
					String imei = etimei.getText().toString();
					IPMAC ipmac = new IPMAC();
					ipmac.set_Depict(depict);
					ipmac.set_MACorTEL(imei);
					ipmac.set_IPMACID(macaddress.get(posiont).get_IPMACID());
					ipmac.set_LoginID(macaddress.get(posiont).get_LoginID());
					IPMACBLL.UpdataPhone(mContext, ipmac);
				}
			});
			add1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int i) {
				}
			});
			add1.show();// 显示对话框
			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);

	}

	@Override
	public void OnViewClick(View v) {
		// TODO Auto-generated method stub

	}

	public static final void dialogTitleLineColor(Dialog dialog, int colors) {
		Context context = dialog.getContext();
		int divierId = context.getResources().getIdentifier(
				"android:id/titleDivider", null, null);
		View divider = dialog.findViewById(divierId);
		divider.setBackgroundColor(colors);
		divider.setBackgroundResource(R.drawable.show_head_toast_bg);
	}

}
